#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>

void handlerSig (int sig)
{
    std::cout << "I am a signal handler, I received signal " << sig << std::endl;
}

int main()
{
    // -9 is the signal number for SIGKILL
    // 9号信号无法捕获，所以无法注册信号处理函数
    for (int i = 1; i <= 32; ++i)
        signal(i, handlerSig);

    int cnt = 0;
    while(true)
    {
        std::cout << "I am a process, I am waiting signal! " << cnt++ << " " << getpid() << std::endl;
        sleep(1);

        // 向进程发送信号
        // kill(getpid(), cnt);
        if (cnt != 9)
            raise(cnt);

        // 即使通过 signal(SIGABRT, SIG_IGN) 尝试忽略 SIGABRT 信号，abort() 仍会强制终止进程
        abort();
    }

    return 0;
}